<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-01 22:12:12;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace application\core;

class View
{

    protected $variables = [];

    /**
     * 分配变量
     *
     * @param string $name
     * @param string $value
     */
    public function assign(string $name, string $value)
    {
        $this->variables[$name] = $value;
    }

    /**
     * 渲染显示
     */
    public function view($page = '')
    {
        extract($this->variables);
        $defaultHeader = sprintf("%s"."modules/"."%s"."/view/common/header.php",APP_PATH,RUN_PATH);
        $defaultFooter = sprintf("%s"."modules/"."%s"."/view/common/footer.php",APP_PATH,RUN_PATH);

        $pathViewFile = "";
        $controllerHeader = "";
        $controllerFooter = "";
        if (! empty($page)) {
            $controllerLayout = sprintf("%s"."modules/"."%s"."/view/"."%s".".php",APP_PATH,strtolower(RUN_PATH),$page);
        } else {
            $pathViewFile = APP_PATH . 'modules/' . strtolower(RUN_PATH) . '/view/' . lcfirst(Filter::$controller);
            // 为了兼容view的大小写
            if (! is_dir($pathViewFile)) {
                $pathViewFile = APP_PATH . 'modules/' . strtolower(RUN_PATH) . '/view/' . Filter::$controller;
            }
            $controllerHeader = $pathViewFile . '/header.php';
            $controllerFooter = $pathViewFile . '/footer.php';
            $controllerLayout = sprintf("%s"."/"."%s".".php",$pathViewFile,Filter::$action);

        }
        // 页头文件
        if (file_exists($controllerHeader)) {
            include "View.php";
        } else {
            if (file_exists($defaultHeader)) {
                include "$defaultHeader";
            }
        }
        // 判断视图文件是否存在
        if (is_file($controllerLayout)) {
            include "$controllerLayout";
        } else {
            if (APP_DEBUG) {
                echo "<h1>无法找到视图文件</h1>";
            }
        }
        // 页脚文件
        if (file_exists($controllerFooter)) {
            include "View.php";
        } else {
            if (file_exists($defaultHeader)) {
                include "$defaultFooter";
            }
        }
        unset($page);
        unset($defaultHeader);
        unset($defaultFooter);
        unset($pathViewFile);
        unset($controllerHeader);
        unset($controllerFooter);
        unset($controllerLayout);
    }
}
